package com.ws.test;
import java.util.Scanner;

/**
 * @author 王硕
 * @version jdk21
 * @className Start
 * @description
 * @date 2024/11/13 16:34
 */

public class Start {
    public static void main(String[] args) {
        SharedBikeMgr.initial(); // 初始化数据
        SharedBikeCompany[] bikeCompanies = new SharedBikeCompany[3];
        bikeCompanies[0] = new SharedBikeCompany("ofo", 10);
        bikeCompanies[1] = new SharedBikeCompany("halo", 10);
        bikeCompanies[2] = new SharedBikeCompany("mobai", 10);

        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("1. 查看所有Bike");
            System.out.println("2. 投放Bike");
            System.out.println("3. 删除Bike");
            System.out.println("4. 借出Bike");
            System.out.println("5. 归还Bike");
            System.out.println("6. 显示借出排行榜");
            System.out.println("7. 退出");
            System.out.print("请选择操作：");
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    displayBikes(bikeCompanies);
                    break;
                case 2:
                    putBike(bikeCompanies, scanner);
                    break;
                case 3:
                    removeBike(bikeCompanies, scanner);
                    break;
                case 4:
                    borrowBike(bikeCompanies, scanner);
                    break;
                case 5:
                    returnBike(bikeCompanies, scanner);
                    break;
                case 6:
                    displayRanking(bikeCompanies);
                    break;
                case 7:
                    System.out.println("退出系统");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效选项，请重新选择");
                    break;
            }
        }
    }

    private static void displayBikes(SharedBikeCompany[] bikeCompanies) {
        for (SharedBikeCompany company : bikeCompanies) {
            SharedBike[] bikes = company.getSharedBikes();
            for (int i = 0; i < company.getSum(); i++) {
                if (bikes[i] != null) {
                    System.out.println("公司：" + company.getBikeCompanyName() + ", 编号：" + bikes[i].getBid() + ", 名称：" + bikes[i].getBname() + ", 状态：" + (bikes[i].getStatus() == 1 ? "可借" : "已借出") + ", 借出时间：" + bikes[i].getBorrowTime());
                }
            }
        }
    }

    private static void putBike(SharedBikeCompany[] bikeCompanies, Scanner scanner) {
        System.out.print("输入单车编号：");
        int bid = scanner.nextInt();
        System.out.print("输入单车名称：");
        String bname = scanner.next();
        System.out.print("输入单车公司编号（1-3）：");
        int companyId = scanner.nextInt() - 1;
        if (companyId >= 0 && companyId < bikeCompanies.length) {
            SharedBikeCompany company = bikeCompanies[companyId];
            SharedBike newBike = new SharedBike(bid, bname);
            company.addBike(newBike);
            System.out.println("投放成功");
        } else {
            System.out.println("无效的公司编号");
        }
    }
    private static void removeBike(SharedBikeCompany[] bikeCompanies, Scanner scanner) {
        System.out.print("输入要删除的单车编号：");
        int bid = scanner.nextInt();
        System.out.print("输入单车公司编号（1-3）：");
        int companyId = scanner.nextInt() - 1;
        if (companyId >= 0 && companyId < bikeCompanies.length) {
            bikeCompanies[companyId].removeBike(bid);
        } else {
            System.out.println("无效的公司编号");
        }
    }

    private static void borrowBike(SharedBikeCompany[] bikeCompanies, Scanner scanner) {
        System.out.print("输入单车编号：");
        int bid = scanner.nextInt();
        System.out.print("输入单车公司编号（1-3）：");
        int companyId = scanner.nextInt() - 1;
        if (companyId >= 0 && companyId < bikeCompanies.length) {
            SharedBikeCompany company = bikeCompanies[companyId];
            for (int i = 0; i < company.getSum(); i++) {
                SharedBike bike = company.getSharedBikes()[i];
                if (bike != null && bike.getBid().equals(bid)) {
                    company.borrowBike(i, "当前时间");
                    System.out.println("借出成功");
                    return;
                }
            }
            System.out.println("未找到单车");
        } else {
            System.out.println("无效的公司编号");
        }
    }

    private static void returnBike(SharedBikeCompany[] bikeCompanies, Scanner scanner) {
        System.out.print("输入单车编号：");
        int bid = scanner.nextInt();
        System.out.print("输入单车公司编号（1-3）：");
        int companyId = scanner.nextInt() - 1;
        if (companyId >= 0 && companyId < bikeCompanies.length) {
            SharedBikeCompany company = bikeCompanies[companyId];
            for (int i = 0; i < company.getSum(); i++) {
                SharedBike bike = company.getSharedBikes()[i];
                if (bike != null && bike.getBid().equals(bid)) {
                    company.returnBike(i, "当前时间");
                    System.out.println("归还成功");
                    return;
                }
            }
            System.out.println("未找到单车");
        } else {
            System.out.println("无效的公司编号");
        }
    }

    private static void displayRanking(SharedBikeCompany[] bikeCompanies) {
        for (SharedBikeCompany company : bikeCompanies) {
            System.out.println("公司名称: " + company.getBikeCompanyName() + ", 借出次数: " + company.getBorrowCount());
        }
    }
}